var stars = [mercury,venus,mars,jupiter,saturn,uranus,neptune,comet];

var sun,mercury,venus,earth,moon,jupiterMoon1,jupiterMoon2,jupiterMoon3,mars,jupiter,saturn,uranus,neptune,comet;
var jupiter_obj3D,jupiterMoon1_obj3D,jupiterMoon2_obj3D,jupiterMoon3_obj3D,mercury_object3D,venus_object3D,earth_obj3D,moon_obj3D,mars_object3D,jupiter_object3D,saturn_object3D,uranus_object3D,neptune_object3D,comet_object3D;
var obj3Ds = [mercury_object3D,venus_object3D,mars_object3D,jupiter_object3D,saturn_object3D,uranus_object3D,neptune_object3D,comet_object3D];
var particleGroup, particleAttributes;

var offsetX = 0;

function updatePlanetsMove()
{
    obj3Ds[0].rotation.z += 1/stars.Mercury.rep*window.speed;//水星公转
    obj3Ds[1].rotation.z += 1/stars.Venus.rep*window.speed;//金星公转
    earth_obj3D.rotation.z += 1/stars.Earth.rep*window.speed;//地球公转

    if (window.loakSwitch)
    {
        window.lockX = calcNewPoint(window.plantData.sd,0,window.plantObj.rotation.z ,"x") - window.plantData.dia*window.moveIndex;
        window.lockY = calcNewPoint(window.plantData.sd,0,window.plantObj.rotation.z ,"y") - window.plantData.dia*window.moveIndex;

        controls.center.x = lockX;
        controls.center.z = lockY;
        controls.center.y = 0;
        if (window.lockCameraSwitch)
        {
            camera.position.x = lockX;
            camera.position.z = lockY;
        }
    }

    moon_obj3D.rotation.z += 1/stars.Moon.rep*window.speed;//月球公转
    obj3Ds[2].rotation.z += 1/stars.Mars.rep*window.speed;//火星公转
    jupiter_obj3D.rotation.z += 1/stars.Jupiter.rep*window.speed;//木星公转

    jupiterMoon1_obj3D.rotation.z += 1/stars.JupiterMoon1.rep*window.speed;//木星卫星公转
    jupiterMoon2_obj3D.rotation.z += 1/stars.JupiterMoon2.rep*window.speed;//木星卫星公转
    jupiterMoon3_obj3D.rotation.z += 1/stars.JupiterMoon3.rep*window.speed;//木星卫星公转

    //obj3Ds[3].rotation.z += 1/stars.Jupiter.rep*window.speed;//木星公转
    obj3Ds[4].rotation.z += 1/stars.Saturn.rep*window.speed;//土球公转
    obj3Ds[5].rotation.z += 1/stars.Uranus.rep*window.speed;//天王星公转
    obj3Ds[6].rotation.z += 1/stars.Neptune.rep*window.speed;//海王星公转

    //obj3Ds[7].rotation.z += 0.02;//哈雷公转

    sun.rotation.y += 1/stars.Sun.rop*window.speed;//太阳自转
    stars[0].rotation.y += 1/stars.Mercury.rop*window.speed;//水星自转
    stars[1].rotation.y += 1/stars.Venus.rop*window.speed;//金星自转
    earth.rotation.y += 1/stars.Earth.rop*window.speed;//地球自转
    moon.rotation.y += 1/stars.Moon.rop*window.speed;//月球自转
    stars[2].rotation.y += 1/stars.Mars.rop*window.speed;;//火星自转
    //stars[3].rotation.y += 0.01;//木星自转
    jupiterMoon1.rotation.y += 1/stars.JupiterMoon1.rop*window.speed;//木星公转
    jupiterMoon2.rotation.y += 1/stars.JupiterMoon2.rop*window.speed;//木星公转
    jupiterMoon3.rotation.y += 1/stars.JupiterMoon3.rop*window.speed;//木星公转

    jupiter.rotation.y += 1/stars.Jupiter.rop*window.speed;//木星自转
    stars[4].rotation.y += 1/stars.Saturn.rop*window.speed;//土球自转
    stars[5].rotation.y += 1/stars.Uranus.rop*window.speed;//天王星自转
    stars[6].rotation.y += 1/stars.Neptune.rop*window.speed;//海王星自转
}

function drawPlanet(radius,segment,axisHelper,index,px,py,pz,image,rea,roa,isHalei,airSize,airColor)
{
    stars[index] = createSphere(radius,segment,image);
    stars[index].position.set(px, py, pz);
    stars[index].rotation.set(roa - Math.PI/2,0,0);
    var orbit = createOrbit(px,200,0,1);
    obj3Ds[index] = new THREE.Object3D();
    obj3Ds[index].add(axisHelper);
    obj3Ds[index].add(stars[index]);
    //添加行星公转轨道
    obj3Ds[index].add(orbit);
    if (isHalei)
    {
        obj3Ds[index].position.set(offsetX,0,3000); //位置在太阳中心
    }
    else
    {
        obj3Ds[index].position.set(offsetX,0,0); //位置在太阳中心

    }
    obj3Ds[index].rotation.set(Math.PI/2,rea,0);
    if (index==4)
    {
        var ring = createRings(radius, 120);
        ring.position.set(px,0,0);
        obj3Ds[index].add(ring);
    }
    scene.add(obj3Ds[index]);

    //大气层
    console.log(index);
    var spriteX = createSprits(airColor,'images/plantImg/glow.png');
    spriteX.scale.set(radius *airSize, radius *airSize, 8);
    spriteX.position.set(px,0,0);
    obj3Ds[index].add(spriteX);
}

function drawSun()
{
    sun = createSphere_0(stars.Sun.dia,500,'images/plantImg/sun.jpg');
    sun.position.set(offsetX,0,0);
    scene.add(sun);
    // 太阳光
    var sunSprite = createSprits(0xFfbd20,'images/plantImg/glow.png');
    sunSprite.scale.set(stars.Sun.dia *6, stars.Sun.dia *6, 8);
    sun.add(sunSprite);
    var sunSprite = createSprits(0x222222,'images/plantImg/glow.png');
    sunSprite.scale.set(stars.Sun.dia *6, stars.Sun.dia *6, 8);
    sun.add(sunSprite);
}

//画木星和三颗卫星
function drawJupiterAndMoons(axisHelper)
{
    jupiter = createSphere(stars.Jupiter.dia,32,'images/plantImg/jupiter.jpg');
    jupiter.position.set(stars.Jupiter.sd,0,0);
    jupiter.rotation.set(stars.Jupiter.roa - Math.PI/2,0,0);
    //木星大气
    var jupiterSprite = createSprits(0xFFFFFF,'images/plantImg/glow.png');//0xFEAB10
    jupiterSprite.scale.set(stars.Jupiter.dia *4, stars.Jupiter.dia *4, 8);
    jupiter.add(jupiterSprite);
    //木星公转轨道
    var jupiter_orbit = createOrbit(stars.Jupiter.sd);
    jupiter_obj3D = new THREE.Object3D();
    jupiter_obj3D.add(axisHelper) ;
    jupiter_obj3D.add(jupiter);
    jupiter_obj3D.add(jupiter_orbit);
    jupiter_obj3D.position.set(offsetX,0,0);  //位置在太阳中心
    jupiter_obj3D.rotation.set(Math.PI/2,stars.Jupiter.rea,0);

    //jupiterMoon1,jupiterMoon2,jupiterMoon3
    //木星卫星1
    jupiterMoon1 = createSphere(stars.JupiterMoon1.dia,16,'images/plantImg/moon.jpg');
    jupiterMoon1.position.set(stars.JupiterMoon1.sd,0,0);
    jupiterMoon1.rotation.set(-Math.PI/2,0,0);

    var jupiterMoon1_orbit = createOrbit(stars.JupiterMoon1.sd);
    jupiterMoon1_obj3D = new THREE.Object3D();
    jupiterMoon1_obj3D.add(axisHelper) ;
    jupiterMoon1_obj3D.add(jupiterMoon1);
    jupiterMoon1_obj3D.add(jupiterMoon1_orbit);
    jupiterMoon1_obj3D.position.set(stars.Jupiter.sd,0,0); //位置在地球中心
    jupiterMoon1_obj3D.rotation.set(stars.JupiterMoon1.rea,0,0);

    jupiter_obj3D.add(jupiterMoon1_obj3D);
    //木星卫星2
    jupiterMoon2 = createSphere(stars.JupiterMoon2.dia,16,'images/plantImg/moon.jpg');
    jupiterMoon2.position.set(stars.JupiterMoon2.sd,0,0);
    jupiterMoon2.rotation.set(-Math.PI/2,0,0);

    var jupiterMoon2_orbit = createOrbit(stars.JupiterMoon2.sd);
    jupiterMoon2_obj3D = new THREE.Object3D();
    jupiterMoon2_obj3D.add(axisHelper) ;
    jupiterMoon2_obj3D.add(jupiterMoon2);
    jupiterMoon2_obj3D.add(jupiterMoon2_orbit);
    jupiterMoon2_obj3D.position.set(stars.Jupiter.sd,0,0); //位置在地球中心
    jupiterMoon2_obj3D.rotation.set(stars.JupiterMoon2.rea,0,0);

    jupiter_obj3D.add(jupiterMoon2_obj3D);

    //木星卫星3
    jupiterMoon3 = createSphere(stars.JupiterMoon3.dia,16,'images/plantImg/moon.jpg');
    jupiterMoon3.position.set(stars.JupiterMoon3.sd,0,0);
    jupiterMoon3.rotation.set(-Math.PI/2,0,0);

    var jupiterMoon3_orbit = createOrbit(stars.JupiterMoon3.sd);
    jupiterMoon3_obj3D = new THREE.Object3D();
    jupiterMoon3_obj3D.add(axisHelper) ;
    jupiterMoon3_obj3D.add(jupiterMoon3);
    jupiterMoon3_obj3D.add(jupiterMoon3_orbit);
    jupiterMoon3_obj3D.position.set(stars.Jupiter.sd,0,0); //位置在地球中心
    jupiterMoon3_obj3D.rotation.set(stars.JupiterMoon3.rea,0,0);

    jupiter_obj3D.add(jupiterMoon3_obj3D);

    scene.add(jupiter_obj3D);

}

//画地球和月亮
function drawEarthAndMoon(axisHelper)
{
    //地球
    earth = createSphere(stars.Earth.dia,32,'images/plantImg/earth.jpg');
    earth.position.set(stars.Earth.sd,0,0);
    earth.rotation.set(stars.Earth.roa - Math.PI/2,0,0);

    //地球大气
    var earthSprite = createSprits(0x87CEEB,'images/plantImg/glow.png');//0xFEAB10
    earthSprite.scale.set(stars.Earth.dia *5, stars.Earth.dia *5, 8);
    earth.add(earthSprite);

    //地球公转轨道
    var earth_orbit = createOrbit(stars.Earth.sd);
    earth_obj3D = new THREE.Object3D();
    earth_obj3D.add(axisHelper) ;
    earth_obj3D.add(earth);
    earth_obj3D.add(earth_orbit);
    earth_obj3D.position.set(offsetX,0,0);  //位置在太阳中心
    earth_obj3D.rotation.set(Math.PI/2,stars.Earth.rea,0);

    //月球
    moon = createSphere(stars.Moon.dia,16,'images/plantImg/moon.jpg');
    moon.position.set(stars.Moon.sd,0,0);
    moon.rotation.set(-Math.PI/2,0,0);
    //月球公转轨道
    var moon_orbit = createOrbit(stars.Moon.sd);
    moon_obj3D = new THREE.Object3D();
    moon_obj3D.add(axisHelper) ;
    moon_obj3D.add(moon);
    moon_obj3D.add(moon_orbit);
    moon_obj3D.position.set(stars.Earth.sd,0,0); //位置在地球中心
    moon_obj3D.rotation.set(stars.Moon.rea,0,0);

    earth_obj3D.add(moon_obj3D);

    scene.add(earth_obj3D);
}

function drawSolarSystem()
{
    var axisHelper = new THREE.AxisHelper(4);
    drawSun();
    drawPlanet(stars.Mercury.dia,15,axisHelper,0,stars.Mercury.sd,0,0,'images/plantImg/mercury.jpg',stars.Mercury.rea,stars.Mercury.roa,false,0,0xFFFFFF);
    drawPlanet(stars.Venus.dia,25,axisHelper,1,stars.Venus.sd,0,0,'images/plantImg/venus.jpg',stars.Venus.rea,stars.Venus.roa,false,0,0xFFFFFF);
    drawEarthAndMoon(axisHelper);
    drawPlanet(stars.Mars.dia,15,axisHelper,2,stars.Mars.sd,0,0,'images/plantImg/mars.jpg',stars.Mars.rea,stars.Mars.roa,false,0,0xFFFFFF);
    drawJupiterAndMoons(axisHelper);
    //drawPlanet(stars.Jupiter.dia,60,axisHelper,3,stars.Jupiter.sd,0,0,'images/plantImg/jupiter.jpg',stars.Jupiter.rea,stars.Jupiter.roa,false,4,0xFFFFFF);
    drawPlanet(stars.Saturn.dia,60,axisHelper,4,stars.Saturn.sd,0,0,'images/plantImg/saturn.jpg',stars.Saturn.rea,stars.Saturn.roa,false,6,0x666666);
    drawPlanet(stars.Uranus.dia,60,axisHelper,5,stars.Uranus.sd,0,0,'images/plantImg/uranus.jpg',stars.Uranus.rea,stars.Uranus.roa,false,4,0x4682B4);
    drawPlanet(stars.Neptune.dia,60,axisHelper,6,stars.Neptune.sd,0,0,'images/plantImg/neptune.jpg',stars.Neptune.rea,stars.Neptune.roa,false,5,0x4682B4);
    //drawPlanet(15,15,axisHelper,7,8*500,0,0,'images/plantImg/moon.jpg',Math.PI/2,0,true,0,0xFFFFFF);
}

